package com.wc.alorithm_blue_bridge._思维.植物生命力;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/4/9 16:18
 * @description
 * https://www.lanqiao.cn/problems/19734/learning/?isWithAnswer=true
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010, M = N << 1;
    // 树状数组, 得到比 i 小的有多少个数
    static int[] tr = new int[N];
    static int[] h = new int[N], e = new int[M], ne = new int[M];
    static int[] a = new int[N];
    // 状态数
    static boolean[] st = new boolean[N];
    static long res = 0;
    static int n, s, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        s = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        for (int i = 1; i < n; i++) {
            int a = sc.nextInt(), b = sc.nextInt();
            add(a, b);
            add(b, a);
        }
        dfs(s, -1, 0);
        out.println(res);
        out.flush();
    }

    /**
     *
     * @param u 当前数
     * @param fa 他爹
     * @param num 上面存在多少个数
     */
    static void dfs(int u, int fa, int num) {
        st[a[u]] = true;
        work(a[u], 1);

        int ans = num + 1;

        // 判断上面是否满足是约数
        for (int i = a[u]; i <= n; i += a[u]) {
            if (st[i]) ans--;
        }

        // 减去上面比我小的个数
        res += ans - get(a[u] - 1);


        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (j == fa) continue;
            dfs(j, u, num + 1);
        }

        work(a[u], -1);
        st[a[u]] = false;
    }

    static void work(int x, int c) {
        for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
    }

    static int get(int x) {
        int res = 0;
        for (int i = x; i > 0; i -= lowbit(i)) res += tr[i];
        return res;
    }

    static int lowbit(int x) {
        return x & -x;
    }

    static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

